package br.com.gsc.helper;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageResizerHelper {
	
	private static final int IMG_WIDTH = 100;
	private static final int IMG_HEIGHT = 100;
	
	public String resizeThisTo100x100(String filename) {
		try {
			BufferedImage originalImage = ImageIO.read(new File("C:/Users/cristiano/workspace/roo/src/main/webapp/img/Logos/"+filename));
			int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
	
			BufferedImage resizeImagePng = resizeImage(originalImage, type);
			ImageIO.write(resizeImagePng, "png", new File("C:/Users/cristiano/workspace/roo/src/main/webapp/img/Logos/"+filename)); 
			
		
		}catch(IOException e){
			System.out.println(e.getMessage());
		}
		return filename.substring(0, filename.length() - 5)+".png";
	}


    public static BufferedImage resizeImage(BufferedImage originalImage, int type){
			BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
			Graphics2D g = resizedImage.createGraphics();
			g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
			g.dispose();
		return resizedImage;	
    }

    private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type){

			BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
			Graphics2D g = resizedImage.createGraphics();
			g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
			g.dispose();	
			g.setComposite(AlphaComposite.Src);
	 
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
			RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			g.setRenderingHint(RenderingHints.KEY_RENDERING,
			RenderingHints.VALUE_RENDER_QUALITY);
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);
	 
		return resizedImage;
    }
}
